﻿Imports Microsoft.Xna.Framework

Namespace Global.Runa.UI.Animation

    Public Class RectangleAnimation
        Inherits AnimationBase

        Public From As Nullable(Of Rectangle)
        Public [To] As Rectangle

        Private LoctionIncremental As Vector2
        Private LoctionValue As Vector2
        Private SizeIncremental As Vector2
        Private SizeValue As Vector2

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal fps As Single)
            MyBase.New(fps)
        End Sub

        Public Overrides Sub BeginAnimation(ByVal Control As IAnimatable, ByVal TargetPropertyName As String)
            If Not Control.Animations.Contains(Me) Then
                MyBase.BeginAnimation(Control, TargetPropertyName)
                If Not Me.From.HasValue Then
                    Me.From = New Nullable(Of Rectangle)(DirectCast(MyBase.TargetProperty.GetValue(Control, Nothing), Rectangle))
                End If
                Me.CalculateIncremental(Me.From.Value, Me.To, MyBase.Duration)
                Me.LoctionValue = New Vector2(CSng(Me.From.Value.X), CSng(Me.From.Value.Y))
                Me.SizeValue = New Vector2(CSng(Me.From.Value.Width), CSng(Me.From.Value.Height))
            End If
        End Sub

        Private Sub CalculateIncremental(ByVal Orgion As Rectangle, ByVal [End] As Rectangle, ByVal Time As TimeSpan)
            Dim TotalFream As Integer = CInt((Time.TotalSeconds * Me.FPS))
            Me.LoctionIncremental = (New Vector2(CSng([End].X), CSng([End].Y)) - New Vector2(CSng(Orgion.X), CSng(Orgion.Y))) / CSng(TotalFream)
            Me.SizeIncremental = (New Vector2(CSng([End].Width), CSng([End].Height)) - New Vector2(CSng(Orgion.Width), CSng(Orgion.Height))) / CSng(TotalFream)
        End Sub

        Public Overrides Sub OnComplete()
            MyBase.TargetProperty.SetValue(MyBase.TargetObject, Me.To, Nothing)
            MyBase.OnComplete()
        End Sub

        Public Overrides Sub Updata(ByVal gameTime As GameTime)
            If MyBase.IsEnabled Then
                If (Not MyBase.EasingFunction Is Nothing) Then
                    Me.LoctionIncremental = DirectCast(MyBase.EasingFunction.GetIncremental(Me.LoctionIncremental, MyBase.Progress, MyBase.Duration), Vector2)
                    Me.SizeIncremental = DirectCast(MyBase.EasingFunction.GetIncremental(Me.SizeIncremental, MyBase.Progress, MyBase.Duration), Vector2)
                End If
                Me.LoctionValue += Me.LoctionIncremental * MyBase.Coefficient
                Me.SizeValue += Me.SizeIncremental * MyBase.Coefficient
                MyBase.TargetProperty.SetValue(MyBase.TargetObject, AnimationHelper.CreatRect(Me.LoctionValue, Me.SizeValue), Nothing)
                MyBase.Updata(gameTime)
            End If
        End Sub

    End Class

End Namespace